/**
 * camelCaseToHumpBack
 *
 * @Description: 中划线转驼峰
 * @Params: str: String
 * @CreateTime: 2023/7/5
 * @Author: Zeman
 *
 */
export const camelCaseToHumpBack = (str) => {
  const strArr = str.split('-');
  const newStrArr = strArr.filter(item => {
    if (item) {
      return item;
    }
  }).map((item, index) => {
    if (index) {
      const itemArr = item.split('');
      itemArr[0] = itemArr[0].toUpperCase();
      item = itemArr.join('');
    }
    return item;
  })
  return newStrArr.join('');
}
/**
 * humpBackToCamelCase
 *
 * @Description: todo 驼峰转中划线
 * @Params: str: String
 * @CreateTime: 2023/7/5
 * @Author: Zeman
 *
 */
export const humpBackToCamelCase = (str) => {
  return str.replace(/([A-Z])/g,"-$1").toLowerCase();
}

/**
 * formatTreeNode
 *
 * @Description: 格式化节点树
 * @Params: vNodeTree: Object
 * @CreateTime: 2023/7/5
 * @Author: Zeman
 *
 */
export const formatTreeNode = (vNodeTree) => {
  if (!(vNodeTree && vNodeTree.childNodes)) {
    return;
  }
  const treeNode = [...vNodeTree.childNodes].map(item => {
    const node = {}
    node.tagName = (item.tagName || item.nodeName).toLowerCase();
    node.nodeType = item.nodeType;
    if (item.nodeType !== 3) {
      node.attr = [...item.attributes].map(attr => {
        const {name, value} = attr;
        return {
          name,
          value
        }
      })
    } else {
      node.textContent = item.textContent;
      node.nodeValue = item.nodeValue;
    }
    if (item.childNodes && [...item.childNodes].length) {
      node.children = formatTreeNode(item);
    }
    return node
  })
  return treeNode
}

/**
 * resolveNodeTree
 *
 * @Description: 解析节点树
 * @Params:
 * @CreateTime: 2023/7/5
 * @Author: Zeman
 *
 */
export const resolveNodeTree = (NodeTree, isChildren) => {
  const treeNode = NodeTree.map(item => {
    if (item.nodeType !== 3 ) {
      const node = document.createElement(item.tagName);
      if (item.attr && item.attr.length) {
        item.attr.forEach(attr => {
          node.setAttribute(attr.name, attr.value);
        })
      }
      if (item.children && item.children.length) {
        node.append(...resolveNodeTree(item.children, true));
      }
      return node;
    } else {
      const value = item.textContent || item.nodeValue;
      return document.createTextNode(value);
    }
  })
  return treeNode;
}

/**
 * getEditorContentLength
 *
 * @Description: 获取编辑器内容长度
 * @Params:
 * @CreateTime: 2023/7/6
 * @Author: Zeman
 *
 */
export const getEditorContentLength = (el) => {
  if (!el.value) {
    return 0;
  }
  const text = el.value.innerText.replace(/["'\n]/g, function(match) {
    return {
      '"': '"',
      "'": "'",
      '\n': ''
    }[match];
  });
  return text.length;
}
/**
 * formatTextToBold
 *
 * @Description: 格式化文本为加粗
 * @Params:
 * @CreateTime: 2023/7/5
 * @Author: Zeman
 *
 */
export const formatTextToBold = () => {

}

/**
 * getLastChildNode
 *
 * @Description: 获取最后一个子节点
 * @Params: el: Element
 * @CreateTime: 2023/7/11
 * @Author: Zeman
 *
 */
export const getLastChildNode = (el) => {
  const childNodes = el.childNodes;
  if (!childNodes.length) {
    return el;
  }
  const lastChildNode = childNodes[childNodes.length - 1];
  return getLastChildNode(lastChildNode);
}